package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.PraiseArticleDao;

import com.qf.lanzhou.blog.api.pojo.entity.Praise;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author jian
 * @Date 2020/12/21 11:25
 * @JDK 1.8
 * @Version 1.0
 */
public class PraiseArticleDaoimpl implements PraiseArticleDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public List<Praise> selectArticle(String userId) {
        String sql="select article_id from t_praise where user_id=? and praise_status=1";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        try {
            return queryRunner.query(sql,new BeanListHandler<Praise>(Praise.class),userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Praise selectByUserIdAndArticleId(String userId, String articleId) {
        Object[] obj={userId,articleId};
        String sql="select praise_id from t_praise where user_id=? and article_id=?";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        Praise praise=null;
        try {
            praise = queryRunner.query(sql, new BeanHandler<Praise>(Praise.class, processor), obj);
            return praise;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
